<template>
  <div class="add-container">
    <el-dialog v-model="dialogVisible" width="500" :show-close="false" :before-close="handleClose">
      <template #header>
        <div class="dialog-header">
          <span>新建日历</span>
          <el-icon @click="handleClose">
            <Close />
          </el-icon>
        </div>
      </template>
      <div class="add-form">
        <el-form :model="form" :rules="rules" ref="ruleFormRef">
          <el-form-item label="日历名称" prop="scheduleName">
            <el-input v-model="form.scheduleName" />
          </el-form-item>
          <el-form-item label="可见范围" prop="schedulePurview">
            <el-select v-model="form.schedulePurview" placeholder="请选择可见范围">
              <el-option label="私有：只有加入的成员才能看见此日历" :value="0" />
              <el-option label="公开：企业所有成员都可以看见此日历" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item label="日历颜色">
            <el-color-picker v-model="form.scheduleColor" />
          </el-form-item>
          <!-- <el-form-item label="日历描述">
                        <el-input v-model="form.desc" type="textarea" />
                    </el-form-item> -->
          <el-form-item label="添加成员" v-if="form.schedulePurview == 0">
            <div class="add-member">
              <el-button :icon="Plus" circle @click="openUserDia" />
              <div class="member-list">
                <el-tag v-for="item in form.member" :key="item.id" closable @close="handleCloseTag(item)"
                  type="primary">{{
                    item.nickName }}</el-tag>
              </div>
            </div>
          </el-form-item>
          <el-form-item label="成员权限" v-if="form.schedulePurview == 1" prop="permissionsId">
            <el-select v-model="form.permissionsId" placeholder="请选择成员权限">
              <el-option label="管理：系统默认权限，包含日历所有权限，不可修改/删除" :value="1" />
              <el-option label="编辑：成员初始加入时默认享有的权限，可修改权限范围" :value="2" />
              <el-option label="只读：日历只读角色" :value="3" />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addCalendar(ruleFormRef)"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
    <SelectUser ref="selectUserRef" @sureDialogTransferClick="sureDialogTransferClick" />
  </div>
</template>

<script lang="ts" setup>
import { Plus } from '@element-plus/icons-vue'
import SelectUser from '../../components/select-user-check/select-user.vue'
import { createCalendar } from '@/api/calendar'
import useUserStore from '@/store/modules/user'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
const dialogVisible = ref(false)
const selectUserRef = ref<any>(null)
const userStore = useUserStore()
const ruleFormRef = ref<FormInstance>()
const form = reactive({
  schedulePurview: 0,
  scheduleName: '',
  scheduleColor: '#409EFF',
  permissionsId: 1,
  member: [],
  userIdList: []
})
const rules = reactive({
  scheduleName: [
    { required: true, message: '请输入日历名称', trigger: 'blur' },
  ],
  schedulePurview: [
    { required: true, message: '请选择可见范围', trigger: 'change' },
  ],
  permissionsId: [
    { required: true, message: '请选择成员权限', trigger: 'change' },
  ],
})


const handleClose = () => {
  dialogVisible.value = false
}
const openDialog = () => {
  let userInfo = [
    {
      userId: userStore.userId,
      userName: userStore.name,
      nickName: userStore.nickname,
    }
  ]
  dialogVisible.value = true
  form.member = [...userInfo]
}
// 选择人员
const openUserDia = () => {
  if (selectUserRef.value) {
    // selectUserRef.value.openDialogTransfer(form.member)
    if (form.member.length > 0) {
      selectUserRef.value.openDialogTransfer(form.member)
    } else {
      selectUserRef.value.openDialogTransfer()
    }
  }
}
// 删除标签
const handleCloseTag = (item: any) => {
  form.member.splice(form.member.indexOf(item), 1)
}
// 确认人员
const sureDialogTransferClick = (data: any) => {
  form.member = data
}

// 新增日历
const addCalendar = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      form.userIdList = form.member.map((item: any) => item.userId)
      const res = await createCalendar({ ...form })
      if (res.code == 200) {
        ElMessage.success('新增成功')
        handleClose()
        emit('getData')
      }
    } else {
      console.log('error submit!', fields)
    }
  })

}
const emit = defineEmits(['getData'])

defineExpose({
  openDialog,
});

</script>

<style lang="scss" scoped>
:deep(.el-dialog__header) {
  padding: 0 !important;
  border-bottom: none !important;
}

:deep(.dialog-header) {
  display: flex;
  justify-content: space-between;
  align-items: center;

  i {
    cursor: pointer;
  }
}

.add-container {
  .add-form {
    padding-top: 20px;

    .add-member {
      display: flex;

      .member-list {
        margin-left: 8px;

        :deep(.el-tag) {
          margin-right: 8px;
        }
      }
    }
  }
}
</style>